<?php

require_once('classes/tools.class.php');

if( !Tools::check_ajax_referer() ) {
    header( 'Location: index.php' );
    exit(0);
}

session_start();

$src = $_REQUEST["source"];
$oper = $_REQUEST["oper"];

if( !isset($src) || $src == '' || !isset($oper) || $oper == '' )
    die("Нарушение формата данных.");
 
if( !($conf = parse_ini_file('config.ini', TRUE)))
    die("Конфигурационный файл не найден");

$_SESSION['sitename'] = isset($conf['Common']['sitename']) ? $conf['Common']['sitename'] : "localhost";
$_SESSION['sitemail'] = isset($conf['Common']['sitemail']) ? $conf['Common']['sitemail'] : "root@localhost";
$_SESSION['adminmail'] = isset($conf['Common']['adminmail']) ? $conf['Common']['adminmail'] : "root@localhost";

$c_charset = isset($conf['DB']['charset']) ? $conf['DB']['charset'] : "utf8";
$dsn = "mysql:host=".$conf['DB']['dbserv'].";".
    "dbname=".$conf['DB']['dbname'].";".
    "charset=".$c_charset.";";

try {
    $db = new PDO( $dsn, $conf['DB']['dbuser'], $conf['DB']['dbpass'] );
	$db->exec('SET CHARACTER SET '.$c_charset);	
} 
catch (PDOException $e) {
    die();
}

switch ($src)
{
	case "CarImages":
        require_once( "classes/carImages.class.php" );
	    $ob = new CarImages($db);
		switch ($oper) {
			case "get":  $ob->GetImage();    break;
			case "edit": $ob->Update();  break;
			case "del":  $ob->Delete();  break;
		}
        break;
        		
    case "ColorDictionary":
        require_once( "classes/colorDic.class.php" );
	    $ob = new ColorDictionary($db);
		switch ($oper) {
			case "add":  $ob->Insert();  break;
			case "edit": $ob->Update();  break;
			case "del":  $ob->Delete();  break;
			case "find": $ob->Find();    break;
			case "tree": $ob->GetList(); break;
			case "page": $ob->GetPage(); break;
		}
        break;
        
    case "VendorDictionary":
        require_once( "classes/vendorDic.class.php" );
		$ob = new VendorDictionary($db);
		switch ($oper) {
			case "add":  $ob->Insert();  break;
			case "edit": $ob->Update();  break;
			case "del":  $ob->Delete();  break;
			case "find": $ob->Find();    break;
			case "tree": $ob->GetList(); break;
			case "page": $ob->GetPage(); break;
		}
        break;
        
    case "ModelDictionary":
        require_once( "classes/modelDic.class.php" );
        $ob = new ModelDictionary($db);
		switch ($oper) {
			case "add":  $ob->Insert();  break;
			case "edit": $ob->Update();  break;
			case "del":  $ob->Delete();  break;
			case "find": $ob->Find();    break;
			case "tree": $ob->GetList(); break;
			case "page": $ob->GetPage(); break;
		}
        break;
        
    case "ModifDictionary":
        require_once( "classes/modifDic.class.php" );
        $ob = new ModifDictionary($db);
		switch ($oper) {
			case "add":  $ob->Insert();  break;
			case "edit": $ob->Update();  break;
			case "del":  $ob->Delete();  break;
			case "find": $ob->Find();    break;
			case "tree": $ob->GetList(); break;
			case "page": $ob->GetPage(); break;
		}
        break;
        
    case "CarCatalogue":
        require_once( "classes/cars.class.php" );
        $ob = new Cars($db);
		switch ($oper) {
			case "add":  $ob->Insert();  break;
			case "edit": $ob->Update();  break;
			case "del":  $ob->Delete();  break;
			case "find": $ob->Find();    break;
			case "tree": $ob->GetList(); break;
			case "page": $ob->GetPage(); break;
			case "last": $ob->GetFirstLast(); break;
		}
        break;

    case "CarPrice":
        require_once( "classes/price.class.php" );
        $ob = new Price($db);
		switch ($oper) {
			case "add":  $ob->Insert();  break;
			case "edit": $ob->Update();  break;
			case "del":  $ob->Delete();  break;
			case "find": $ob->Find();    break;
			case "tree": $ob->GetList(); break;
			case "page": $ob->GetPage(); break;
		}
        break;
        
    case "CarTree":
        if( $_REQUEST["node_id"] === '0' ) {
            require_once( "classes/vendorDic.class.php" );
            $ob = new VendorDictionary($db);            
        }
        else {
			$subnodes = explode('.',$_REQUEST["node_id"]);
			if( count( $subnodes ) == 1 ) {
				require_once( "classes/modelDic.class.php" );
				$ob = new ModelDictionary($db);
			}
			else {
				require_once( "classes/modifDic.class.php" );
				$ob = new ModifDictionary($db);
			}
        }
		switch ($oper) {
			case "add":  $ob->Insert();  break;
			case "edit": $ob->Update();  break;
			case "del":  $ob->Delete();  break;
			case "find": $ob->Find();    break;
			case "tree": $ob->GetList(); break;
			case "page": $ob->GetPage(); break;
		}
        break;
        
    case "Customer":
        require_once( "classes/customer.class.php" );
        $ob = new Customer($db);
		switch ($oper) {
			case "register": $ob->Register(); break;
			case "login": $ob->Login(); break;
			case "restore": $ob->Restore(); break;
			case "logout": $ob->Logout(); break;
			case "read": $ob->ReadProfile(); break;
			case "update": $ob->UpdateProfile(); break;
			case "amail": $ob->AdminMail(); break;
		}
        break;
        
	case 'CarUtils':
		require_once( "classes/utils.class.php" );
		$ob = new CarUtils($db);
		switch($oper) {
			case 'cities': $ob->GetCitiesList(); break;
		}
		break;
		
	default:
        die("Нарушение формата данных.");
}

?>